package com.demo.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.demo.R;
import com.demo.adapter.AdapterSong;
import com.demo.item.SongItem;
import com.demo.task.BitmapDownload;
import com.demo.task.TaskDownload;

import java.util.ArrayList;
import java.util.Calendar;

public class MainActivity extends ActionBarActivity {
    private ListView listSong;
    private Button btnReset;
    private AdapterSong adapterSong;
    private ImageView imgTest;
    private Handler handler;
    private Button btnAdd;

    private AdapterView.OnItemClickListener onSongItemClick = new AdapterView.OnItemClickListener(

    ) {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),"Position = " + position + " clicked",Toast.LENGTH_SHORT).show();
            SongItem songItem = adapterSong.getItem(position); // Send object > ActivityDetail
        }
    };

    View.OnClickListener onResetClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String imagePath = "http://fc09.deviantart.net/fs71/i/2012/034/2/8/sexy_girl_by_dgeblade-d4ohx6w.jpg";

//            TaskDownload taskDownload = new TaskDownload(handler,imagePath){
//                @Override
//                public void onDownloadFinish(Bitmap bitmap) {
//                    imgTest.setImageBitmap(bitmap);
//                }
//            };
//            taskDownload.start();

            BitmapDownload bitmapDownload = new BitmapDownload() {
                @Override
                public void onDownloadFinish(Bitmap bitmap) {
                    imgTest.setImageBitmap(bitmap);
                }

                @Override
                public void onError() {
                    Toast.makeText(getApplicationContext(),"Download fail",Toast.LENGTH_SHORT).show();
                }
            };
            bitmapDownload.execute(imagePath);

        }
    };

    private View.OnClickListener onAddItemClicked = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar calendar = Calendar.getInstance();
            StringBuffer stringBuffer = new StringBuffer();
            String test = calendar.getTimeZone().getDisplayName();

//            Log.e("TEST",calendar.getTime().toString());
//            stringBuffer.append(calendar.get(Calendar.DAY_OF_MONTH)).append("|").append(calendar.get(Calendar.MONTH)).append("|").append(calendar.get(Calendar.HOUR_OF_DAY)).append("|").append(calendar.get(Calendar.MINUTE)).append("|").append(calendar.get(Calendar.MILLISECOND));
            SongItem songItem = new SongItem(calendar.getTime().toString());
            adapterSong.add(songItem);
            adapterSong.notifyDataSetChanged();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        handler = new Handler();
//        {
//            @Override
//            public void handleMessage(Message msg) {
//            Bitmap  bitmap = (Bitmap) msg.obj;
//            imgTest.setImageBitmap(bitmap);
//        }
//        }

        listSong = (ListView) findViewById(R.id.list_song);
        imgTest = (ImageView)findViewById(R.id.img_test);
        btnReset = (Button) findViewById(R.id.btn_reset);
        btnAdd = (Button) findViewById(R.id.btn_add);

        ArrayList<SongItem> items = new ArrayList<SongItem>();
//        for (int i = 0; i < 100; i++) {
//            items.add(new SongItem("Position " + i));
//        }
        adapterSong = new AdapterSong(getApplicationContext(), items);
        listSong.setAdapter(adapterSong);
        listSong.setOnItemClickListener(onSongItemClick);

        btnReset.setOnClickListener(onResetClicked);
        btnAdd.setOnClickListener(onAddItemClicked);
    }
}
